//
//  FKRefundStatusCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/3.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKRefundStatusCell.h"
#import "FKOperationBtnStatus.h"

@interface FKRefundStatusCell ()

@property (nonatomic, weak) UILabel *statusNameLabel;
@property (nonatomic, weak) UILabel *statusDescLabel;
@property (nonatomic, weak) UILabel *descLabel;
@property (nonatomic, strong) NSArray *dotViews;
@property (nonatomic, strong) NSArray *valueLabels;

//@property (nonatomic, weak) UIButton *cancelBtn;
@property (nonatomic, strong) NSArray *itemBtns;

@end

@implementation FKRefundStatusCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.clipsToBounds = true;
    
    UIView *topBgView = [[UIView alloc] init];
    [self.contentView addSubview:topBgView];
    topBgView.backgroundColor = fkMainColor;
    
    UILabel *statusNameLabel = [UILabel fk_labelWithFont:fk_adjustFont(16) textColor:[UIColor whiteColor]];
    [topBgView addSubview:statusNameLabel];
    self.statusNameLabel = statusNameLabel;
    
    UILabel *statusDescLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:[UIColor whiteColor]];
    [topBgView addSubview:statusDescLabel];
    self.statusDescLabel = statusDescLabel;
    
    UILabel *descLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor666666];
    [self.contentView addSubview:descLabel];
    self.descLabel = descLabel;
    
    UIView *separator = [[UIView alloc] init];
    [self.contentView addSubview:separator];
    separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *lastLabel = nil;
    __block CGFloat dotTopOffset = [@"测试" sizeWithAttributes:@{NSFontAttributeName : fk_adjustFont(12)}].height * 0.5 - fk_adjustW(2.5);
    NSMutableArray *dotViews = [NSMutableArray array];
    NSMutableArray *valueLabels = [NSMutableArray array];
    for (NSInteger index = 0; index < 4; index++) {
        UIView *dotView = [[UIView alloc] init];
        [self.contentView addSubview:dotView];
        [dotViews addObject:dotView];
        dotView.backgroundColor = [UIColor fk_colorWithHex:0xD7D7D7];
        
        UILabel *tipLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:[UIColor fk_colorWithHex:0xA2A2A2]];
        [self.contentView addSubview:tipLabel];
        [valueLabels addObject:tipLabel];
        
        if (index == 0) {
            [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(fk_adjustW(35));
                make.right.offset(-12);
                make.top.equalTo(separator.mas_bottom).offset(fk_adjustW(15));
            }];
        } else {
            [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(lastLabel);
                make.top.equalTo(lastLabel.mas_bottom).offset(fk_adjustW(10));
            }];
        }
        lastLabel = tipLabel;
        [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(fk_adjustW(5));
            make.left.offset(fk_adjustW(18));
            make.top.equalTo(tipLabel).offset(dotTopOffset);
        }];
    }
    self.valueLabels = valueLabels;
    self.dotViews = dotViews;
    
    NSMutableArray *itemBtns = [NSMutableArray array];
    CGFloat itemW = fk_adjustW(100);
    CGFloat itemH = fk_adjustW(30);
    UIButton *lastItem = nil;
    for (NSInteger index = 0; index < 2; index++) {
        UIButton *itemBtn = [UIButton fk_btnWithTarget:self action:@selector(itemBtnClicked:) font:fk_adjustFont(13) normalTitle:@"取消申请" normalTitleColor:fkColor666666 selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
        [self.contentView addSubview:itemBtn];
        [itemBtns addObject:itemBtn];
        [itemBtn fk_viewCornerRadius:fk_adjustW(15) borderWidth:1 borderColor:[UIColor fk_colorWithHex:0xBEBEBE]];
        
        if (index == 0) {
            [itemBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(itemW);
                make.height.offset(itemH);
                make.right.offset(-12);
                make.bottom.offset(fk_adjustW(-15));
            }];
        } else {
            [itemBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.bottom.equalTo(lastItem);
                make.right.equalTo(lastItem.mas_left).offset(-8);
            }];
        }
        lastItem = itemBtn;
    }
    self.itemBtns = itemBtns;
    
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(fk_adjustW(100));
    }];
    [statusNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(fk_adjustW(25));
        make.bottom.equalTo(topBgView.mas_centerY).offset(-8);
    }];
    [statusDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(statusNameLabel);
        make.right.offset(-12);
        make.top.equalTo(topBgView.mas_centerY).offset(8);
    }];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.height.offset(0.8);
        make.top.equalTo(topBgView.mas_bottom).offset(fk_adjustW(45));
    }];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(separator);
        make.top.equalTo(topBgView.mas_bottom).offset(0);
        make.bottom.equalTo(separator.mas_top);
    }];
}

- (void)itemBtnClicked:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(refundStatusCell:didClickedStatusBtn:)]) {
        [self.delegate refundStatusCell:self didClickedStatusBtn:btn.titleLabel.text];
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        self.statusNameLabel.text = data[@"title"];
        self.statusDescLabel.text = data[@"desc"];
        self.descLabel.text = data[@"long_title"];
        
        NSArray *btnStatus = data[@"btnStatus"];
        for (NSInteger index = 0; index < self.itemBtns.count; index++) {
            UIButton *itemBtn = self.itemBtns[index];
            if (index < btnStatus.count) {
                FKOperationBtnStatus *status = btnStatus[index];
                itemBtn.hidden = false;
                [itemBtn setTitle:status.title forState:UIControlStateNormal];
                [itemBtn setTitleColor:status.themeColor forState:UIControlStateNormal];
                itemBtn.layer.borderColor = status.themeColor.CGColor;
            } else {
                itemBtn.hidden = true;
            }
        }
        
        NSArray *desc_arr = data[@"desc_arr"];
        for (NSInteger index = 0; index < self.valueLabels.count; index++) {
            UIView *dotView = self.dotViews[index];
            UILabel *valueLabel = self.valueLabels[index];
            if (index < desc_arr.count) {
                dotView.hidden = false;
                valueLabel.hidden = false;
                valueLabel.text = desc_arr[index];
            } else {
                dotView.hidden = true;
                valueLabel.hidden = true;
            }
        }
    }
}

@end
